package cn.ekuma.epos.panel;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.CardLayout;

import javax.swing.JToolBar;

import e.odbo.data.dao.I_DataLogic;
import e.odbo.data.bean.I_ViewBean;
import cn.ekuma.data.ui.swing.AbstractDTOTableModel;

import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.loader.ui.JQBFParametersPanel;
import com.openbravo.data.model.Field;

import javax.swing.BoxLayout;
import javax.swing.JTextField;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.ListSelectionModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.model.RowNumTableModelWapper;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JSplitPane;


public abstract class AbstractJFinderPanel <T extends I_ViewBean> extends JPanel  {
protected JPanel jNavigator;
private JPanel jNavigatorToolBar;
private JButton button;
protected JPanel panel_1;
protected JTextField inputCode;
protected CardLayout cl1;
protected JXTable jXTable1;
protected I_DataLogic dlSales;
protected AbstractDTOTableModel<T> tableModel;
protected Object[] beforeFilterParas;
protected int timeCycleType;
private JScrollPane scrollPane;
protected JTabbedPane linkTabPanel;
private JSplitPane splitPane;
private JLabel lblNewLabel;
/**
* Create the panel.
*/
public AbstractJFinderPanel(AbstractDTOTableModel<T> tableModel,I_DataLogic dlSales) {
this.tableModel=tableModel;
initComponents();
jXTable1.setModel(new RowNumTableModelWapper(tableModel));
cl1 = (CardLayout) (jNavigator.getLayout());
this.dlSales =dlSales;
configNavigator(jNavigatorToolBar,jNavigator);
Field[] field=getQBFFields();
if(field!=null){
	JQBFParametersPanel qbfPanel=new JQBFParametersPanel(){

		@Override
		public void doQBFParametersFilter(QBFParameters paras) {
			   query(paras);
		}
    };
    qbfPanel.setShowFieldCount(6);
    qbfPanel.setFields(field);
    jNavigator.add(qbfPanel,"QBFPanel");
}
}


private void initComponents() {
setLayout(new BorderLayout(0, 0));

JPanel panel = new JPanel();
add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));

jNavigatorToolBar = new JPanel();
panel.add(jNavigatorToolBar, BorderLayout.CENTER);
jNavigatorToolBar.setLayout(new BoxLayout(jNavigatorToolBar,
		BoxLayout.LINE_AXIS));

button = new JButton(new javax.swing.ImageIcon(getClass().getResource(
		"/com/openbravo/images/search.png")));
button.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		jNavigator.setVisible(!jNavigator.isVisible());
		if(jNavigator.isVisible()){
			cl1.show(jNavigator, "QBFPanel");
		}
	}
});

panel_1 = new JPanel();
jNavigatorToolBar.add(panel_1);
lblNewLabel = new JLabel("快查");
lblNewLabel.setPreferredSize(new Dimension(60, 15));
panel_1.add(lblNewLabel);

inputCode = new JTextField();
inputCode.setPreferredSize(new Dimension(100, 21));
panel_1.add(inputCode);
inputCode.addActionListener(new ActionListener(){
	@Override
	public void actionPerformed(ActionEvent e) {
		m_jBarcode1ActionPerformed(e);
		inputCode.selectAll();
	}
});

JButton button_3 = new JButton(new ImageIcon(
		AbstractJFinishingPanel.class
				.getResource("/com/openbravo/images/apply.png")));
button_3.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		m_jBarcode1ActionPerformed(e);
	}
});
panel_1.add(button_3);

jNavigatorToolBar.add(button);

jNavigator = new JPanel();
jNavigator.setPreferredSize(new Dimension(200, 10));
add(jNavigator, BorderLayout.EAST);
jNavigator.setLayout(new CardLayout(0, 0));


splitPane = new JSplitPane();
splitPane.setOneTouchExpandable(true);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
add(splitPane, BorderLayout.CENTER);

scrollPane = new JScrollPane();
splitPane.setLeftComponent(scrollPane);

jXTable1 = new JXTable();
jXTable1.setColumnControlVisible(true);
jXTable1.setAutoCreateRowSorter(true);
jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
jXTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
scrollPane.setViewportView(jXTable1);

linkTabPanel = new JTabbedPane(JTabbedPane.TOP);
splitPane.setRightComponent(linkTabPanel);
splitPane.setDividerLocation(500);

jNavigator.setVisible(false);
}


protected abstract void m_jBarcode1ActionPerformed(ActionEvent e);

protected abstract void configNavigator(JPanel jNavigatorToolBar, JPanel jNavigator);

protected abstract void configToolBar(JToolBar jToolBar12);

protected  abstract void query(QBFParameters qbfParameters);

protected  abstract Field[] getQBFFields();

public T getSelectedObj(){
int selectedRow = jXTable1.getSelectedRow();
if (selectedRow >= 0)
	return tableModel.getObj(jXTable1.convertRowIndexToModel(selectedRow));
return null;
}

}
